/*
 * FarmConfig.cpp
 *
 *  Created on: 2013-1-22
 *      Author: heqiu
 */

#include "Global.h"

void FarmConfig::getBuildings() {
	TiXmlHandle docHandle(&m_xml);
	TiXmlHandle handle = docHandle.FirstChildElement("root").FirstChildElement(
			"building");
	TiXmlNode* node = 0;
	for (node = handle.ToNode()->FirstChild("node"); node;
			node = node->NextSibling("node")) {
		UnitBuilding * pNode = new UnitBuilding();
		pNode->m_iId = strtoul(node->FirstChildElement("id")->GetText(), NULL,
				10);
		pNode->m_stPos.m_iX = strtoul(
				node->FirstChildElement("pos_x")->GetText(), NULL, 10);
		pNode->m_stPos.m_iY = strtoul(
				node->FirstChildElement("pos_y")->GetText(), NULL, 10);
		pNode->m_iType = strtoul(node->FirstChildElement("type")->GetText(),
				NULL, 10);
		pNode->m_xLen = strtoul(node->FirstChildElement("len_x")->GetText(),
				NULL, 10);
		pNode->m_yLen = strtoul(node->FirstChildElement("len_y")->GetText(),
				NULL, 10);
		g_GlobalData.g_BuildingManager.addBuildingPtr(pNode);
	}
}

void FarmConfig::getBuildingType() {
	TiXmlHandle docHandle(&m_xml);
	TiXmlHandle handle = docHandle.FirstChildElement("root").FirstChildElement(
			"building-type");
	TiXmlNode* node = 0;
	for (node = handle.ToNode()->FirstChild("node"); node;
			node = node->NextSibling("node")) {
		BuildingType * pNode = new BuildingType();
		pNode->m_iType = strtoul(node->FirstChildElement("id")->GetText(), NULL,
				10);
		pNode->m_sName = node->FirstChildElement("name")->GetText();
		g_GlobalData.g_BuildingTypeManager.addBuildingTypePtr(pNode);
	}
}

void FarmConfig::getFarmControl() {
	TiXmlHandle docHandle(&m_xml);
	TiXmlHandle handle = docHandle.FirstChildElement("root").FirstChildElement(
			"control");
	TiXmlNode* node = 0;
	for (node = handle.ToNode()->FirstChild("node"); node;
			node = node->NextSibling("node")) {
		string sId = node->FirstChildElement("id")->GetText();
		if (strcmp(sId.c_str(), "add_building") == 0) {
			string sName = node->FirstChildElement("name")->GetText();
			int iX = atoi(node->FirstChildElement("x-pixel-pos")->GetText());
			int iY = atoi(node->FirstChildElement("y-pixel-pos")->GetText());
			g_GlobalData.g_FarmControl.m_ControlAddBuilding.init(sName.c_str(),
					iX, iY);
		}
	}
}

